package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Storehouse;
import com.hqyj.dao.StorehouseMapper;
import com.hqyj.service.IStorehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class StorehouseServiceImpl implements IStorehouseService {

    //注入StorehouseMapper
    @Autowired(required = false)
    StorehouseMapper storehouseMapper;

    //加载
    @Override
    public HashMap<String, Object> select(Storehouse storehouse) {
        //创建HashMap对象
        HashMap<String ,Object> map = new HashMap<>();
        //定义分页对象
        Page<Storehouse> page1 = new Page<>(storehouse.getPage(),storehouse.getRow());

        //条件构造器，添加条件，没有就为null
        QueryWrapper<Storehouse> queryWrapper = new QueryWrapper<>();

        //查询数据库
        Page<Storehouse> storehouseList = storehouseMapper.selectPage(page1, null);
        //当前页集合
        map.put("list",storehouseList.getRecords());
        //总页数
        map.put("totalPage",storehouseList.getPages());
        //总条数
        map.put("total",storehouseList.getTotal());
        //当前页
        map.put("curPage",storehouse.getPage());
        //如果当前页为1，则上一页为也为1
        if (storehouseList.getCurrent() == 1){
            //上一页
            map.put("prePage",1);
        }else {
            map.put("prePage",storehouse.getPage()-1);
        }
        //如果当前页为最后一页，则下一页为也为最大页
        if (storehouse.getPage() == storehouseList.getPages()){
            //下一页
            map.put("nextPage",storehouseList.getPages());
        }else {
            map.put("nextPage",storehouse.getPage()+1);
        }
        //显示每页条数
        map.put("row",storehouse.getRow());

        return map;
    }

    //新增
    @Override
    public HashMap<String, Object> add(Storehouse storehouse) {
        HashMap<String,Object> map = new HashMap<>();
        int num = storehouseMapper.insert(storehouse);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Storehouse storehouse) {
        HashMap<String,Object> map = new HashMap<>();
        int num = storehouseMapper.updateById(storehouse);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(int storehouseId) {
        HashMap<String,Object> map = new HashMap<>();
        int num = storehouseMapper.deleteById(storehouseId);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }
}
